#### Uncertainty and Conflict Termination Preferences ####
######### Reproduction materials (Main text) #############

set.seed(1310)

# load packages
source("code/packages.r")

#list
conflict_list <- c("5665", "18001", "23466", "52835", "76109", "76275", "86568") #Municipalities in Espada de Honor (Matanock2018, Tellez2018)

# load data
load("data/survey_df.RData")
polls_df <- readr::read_csv("data/polls.csv")
concessions_tab_df <- readxl::read_xlsx("data/concessions-table.xlsx")


## -----------------------------------------------------------------
## Table 1. Items included in concessions composite measure --------
## -----------------------------------------------------------------

concessions_tab_df %>%
  gt::gt() %>%
  gtExtras::gt_theme_nytimes() %>%
  gt::fmt_markdown(columns = c("Item Type", "Question", "Change")) %>%
  gt::tab_style(
    style = list(cell_text(color = "#000", align = "center",v_align = "middle", weight = "bold")),
    locations = cells_column_labels(columns = c(everything())
    )) %>%
  gt::cols_align(
      align = c("center"),
      columns = c("Item Type", "Pre", "Post", "Change")
    ) %>%
  gt::tab_row_group(label = "  ",
                    rows = 3:7) %>%
  gt::tab_row_group(label = " ",
                    rows = 2) %>%
  gt::tab_row_group(label = "",
                    rows = 1) %>%
  gt::tab_options(row_group.font.size = 15,
                  row_group.font.weight = "bold") %>% 
  gt::cols_width(
    "Item Type" ~ px(100),
    c("Pre","Post","Change") ~ px(80),
    "Question" ~ px(400)
                  ) %>% 
  gt::gtsave("figures/table-1-concessions-table.png")

## -----------------------------------------------------------------
## Figure 3. Models support-agreement, negotiated-end, and concessions with FARC -------
## -----------------------------------------------------------------

# Support agreement (FARC) --
summary(mod_support_agreement_1 <- estimatr::lm_robust(scaled_support_agreement ~ d , data = survey_df))
summary(mod_support_agreement_2 <- estimatr::lm_robust(scaled_support_agreement ~ d + age + female + edu + urban + estrato + votesantos, data = survey_df))
summary(mod_support_agreement_3 <- estimatr::lm_robust(scaled_support_agreement ~ d + age + female + edu + urban + estrato + votesantos + victim_ruv + relative_farc_vict + conflict_zone, data = survey_df))

## Negotiated-end (FARC) --
summary(mod_farc_rb_1 <- estimatr::lm_robust(neg_farc ~ d , data = survey_df))
summary(mod_farc_rb_2 <- estimatr::lm_robust(neg_farc ~ d + age + female + edu + urban + estrato + votesantos, data = survey_df))
summary(mod_farc_rb_3 <- estimatr::lm_robust(neg_farc ~ d + age + female + edu + urban + estrato + votesantos + victim_ruv + relative_farc_vict + conflict_zone, data = survey_df))

## Willingness to give concessions (composite) --
summary(mod_conc_rb_agg_1 <- estimatr::lm_robust(conce_agg_scale ~ d, data = survey_df))
summary(mod_conc_rb_agg_2 <- estimatr::lm_robust(conce_agg_scale ~ d + age + female + edu + urban + estrato + votesantos, data = survey_df))
summary(mod_conc_rb_agg_3 <- estimatr::lm_robust(conce_agg_scale ~ d + age + female + edu + urban + estrato + votesantos + victim_ruv + relative_farc_vict + conflict_zone, data = survey_df))

fig_3_df <- dplyr::bind_rows(
  mod_support_agreement_1 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Naive", model = "Support for signed\npeace agreement"),
  mod_support_agreement_2 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Demographic", model = "Support for signed\npeace agreement"),
  mod_support_agreement_3 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Demographic + Conflict", model = "Support for signed\npeace agreement"),
  mod_farc_rb_1 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Naive", model = "Negotiated-end\n(FARC)"),
  mod_farc_rb_2 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Demographic", model = "Negotiated-end\n(FARC)"),
  mod_farc_rb_3 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Demographic + Conflict", model = "Negotiated-end\n(FARC)"),
  mod_conc_rb_agg_1 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Naive", model = "FARC concessions\n(Composite scaled)"),
  mod_conc_rb_agg_2 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Demographic", model = "FARC concessions\n(Composite scaled)"),
  mod_conc_rb_agg_3 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Demographic + Conflict", model = "FARC concessions\n(Composite scaled)"),
) %>% dplyr::filter(term == "d") %>%
  dplyr::mutate(model = forcats::fct_relevel(model, c("Support for signed\npeace agreement", "Negotiated-end\n(FARC)", "FARC concessions\n(Composite scaled)")),
                type = forcats::fct_relevel(type, c("Demographic + Conflict", "Demographic", "Naive")),
                estimate_label = dplyr::case_when(type == "Naive" & outcome == "scaled_support_agreement" ~ paste0(round(estimate, 2), "sd\n[",round(conf.low, 2),",",round(conf.high, 2),"]"),
                                                  type == "Naive" & outcome == "neg_farc" ~ paste0(round(estimate*100, 1), "p.p\n[",round(conf.low, 2),",",round(conf.high, 2),"]"),
                                                  type == "Naive" & outcome == "conce_agg_scale" ~ paste0(round(estimate, 2), "sd\n[",round(conf.low, 2),",",round(conf.high, 2),"]"),
                                                  outcome == "scaled_support_agreement" ~ paste0(round(estimate, 2),"\n[",round(conf.low, 2),",",round(conf.high, 2),"]"),
                                                  outcome == "neg_farc" ~ paste0(round(estimate*100, 1),"\n[",round(conf.low, 2),",",round(conf.high, 2),"]"),
                                                  outcome == "conce_agg_scale" ~ paste0(round(estimate, 2),"\n[",round(conf.low, 2),",",round(conf.high, 2),"]")
                                                  )) 

fig_3 <- ggplot(fig_3_df, aes(x = type, y = estimate, color = type, shape = type, fill = type)) +
  geom_point(size = 2.5) +
  geom_segment(aes(x = type, xend = type, y = conf.low, yend = conf.high), size = 0.5) +
  geom_hline(yintercept = 0, linetype = "longdash", color = "gray60") +
  facet_wrap(~model, labeller = label_value) +
  coord_flip() +
  geom_text(
    mapping = aes(label = estimate_label),
    hjust = 0.5,
    vjust = -0.5,
    size = 3.2,
    fontface = "bold",
    lineheight = .7) +
  theme_bw() +
  scale_y_continuous(limits = c(-.35,.35)) +
  scale_color_manual(guide = guide_legend(reverse = TRUE),
                     values = c("#000000", "darkgray", "#0065b8")) +
  scale_fill_manual(guide = guide_legend(reverse = TRUE),
                    values = c("#000000", "darkgray", "#0065b8")) +
  scale_shape_manual(values = c(21, 22, 23),
                     guide = guide_legend(reverse = TRUE)) +
  theme(axis.text.y= element_blank(),
        axis.ticks.y = element_blank(),
        legend.position = "bottom", 
        legend.title = element_blank(),
        strip.text.y.left = element_text(angle = 0),
        strip.background = element_rect(fill = "white"),
        strip.text = element_text(colour = 'navy', face = "bold"),
        text = element_text(family = "Roboto Condensed",
                            size = 14)) +
  labs(x = "",
       y = "Estimate")

ggsave(fig_3, file = "figures/fig-3-main-models.png", width = 4, height = 1.8,limitsize = FALSE, units = "cm", dpi = 300, scale = 5)

## -----------------------------------------------------------------
## Figure 4. Models with theoretically unrelated outcomes ----------
## -----------------------------------------------------------------

# Negotiated-end (BACRIM) --
summary(mod_bacrim_1 <- estimatr::lm_robust(neg_bacrim ~ d , data = survey_df))
summary(mod_bacrim_2 <- estimatr::lm_robust(neg_bacrim ~ d + age + female + edu + urban + estrato + votesantos, data = survey_df))
summary(mod_bacrim_3 <- estimatr::lm_robust(neg_bacrim ~ d + age + female + edu + urban + estrato + votesantos + victim_ruv + relative_farc_vict + conflict_zone, data = survey_df))

## Willingness to give NON-FARC concessions (composite) --
summary(mod_no_farc_conc_rb_agg_1 <- estimatr::lm_robust(no_farc_conce_agg_scale ~ d, data = survey_df))
summary(mod_no_farc_conc_rb_agg_2 <- estimatr::lm_robust(no_farc_conce_agg_scale ~ d + age + female + edu + urban + estrato + votesantos, data = survey_df))
summary(mod_no_farc_conc_rb_agg_3 <- estimatr::lm_robust(no_farc_conce_agg_scale ~ d + age + female + edu + urban + estrato + votesantos + victim_ruv + relative_farc_vict + conflict_zone, data = survey_df))

fig_4_df <- dplyr::bind_rows(
  mod_bacrim_1 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Naive", model = "Negotiated-end\n(Bacrim)"),
  mod_bacrim_2 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Demographic", model = "Negotiated-end\n(Bacrim)"),
  mod_bacrim_3 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Demographic + Conflict", model = "Negotiated-end\n(Bacrim)"),
  mod_no_farc_conc_rb_agg_1 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Naive", model = "Non-FARC Provisions\n(Composite Scaled)"),
  mod_no_farc_conc_rb_agg_2 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Demographic", model = "Non-FARC Provisions\n(Composite Scaled)"),
  mod_no_farc_conc_rb_agg_3 %>% broom::tidy(conf.int = T) %>% dplyr::mutate(type = "Demographic + Conflict", model = "Non-FARC Provisions\n(Composite Scaled)")
) %>% dplyr::filter(term == "d") %>%
  dplyr::mutate(model = forcats::fct_relevel(model, c("Negotiated-end\n(Bacrim)", "Non-FARC Provisions\n(Composite Scaled)")),
                type = forcats::fct_relevel(type, c("Demographic + Conflict", "Demographic", "Naive")),
                estimate_label = dplyr::case_when(type == "Naive" & outcome == "neg_bacrim" ~ paste0(round(estimate*100, 1), "p.p\n[",round(conf.low, 2),",",round(conf.high, 2),"]"),
                                                  type == "Naive" & outcome == "no_farc_conce_agg_scale" ~ paste0(round(estimate, 2), "sd\n[",round(conf.low, 2),",",round(conf.high, 2),"]"),
                                                  outcome == "neg_bacrim" ~ paste0(format(round(estimate*100, 1),nsmall=1),"\n[",round(conf.low, 2),",",round(conf.high, 2),"]"),
                                                  outcome == "no_farc_conce_agg_scale"~ paste0(round(estimate, 2),"\n[",round(conf.low, 2),",",round(conf.high, 2),"]")
                ))

fig_4 <- ggplot(fig_4_df, aes(x = type, y = estimate, color = type, shape = type, fill = type)) +
  geom_point(size = 2.5) +
  geom_segment(aes(x = type, xend = type, y = conf.low, yend = conf.high), size = 0.5) +
  geom_hline(yintercept = 0, linetype = "longdash", color = "gray60") +
  facet_wrap(~model, ncol=2, labeller = label_value) +
  coord_flip() +
  geom_text(
    mapping = aes(label = estimate_label),
    hjust = 0.5,
    vjust = -0.5,
    size = 3.2,
    fontface = "bold",
    lineheight = .7) +
  theme_bw() +
  scale_y_continuous(limits = c(-.35,.35)) +
  scale_color_manual(guide = guide_legend(reverse = TRUE),
                     values = c("#000000", "darkgray", "#0065b8")) +
  scale_fill_manual(guide = guide_legend(reverse = TRUE),
                    values = c("#000000", "darkgray", "#0065b8")) +
  scale_shape_manual(values = c(21, 22, 23),
                     guide = guide_legend(reverse = TRUE)) +
  theme(axis.text.y= element_blank(),
        axis.ticks.y = element_blank(),
        legend.position = "bottom", 
        legend.title = element_blank(),
        strip.text.y.left = element_text(angle = 0),
        strip.background = element_rect(fill = "white"),
        strip.text = element_text(colour = 'navy', face = "bold"),
        text = element_text(family = "Roboto Condensed",
                            size = 14)) +
  labs(x = "",
       y = "Estimate")

ggsave(fig_4, file = "figures/fig-4-robustness-models.png", width = 2.7, height = 1.8,limitsize = FALSE, units = "cm", dpi = 300, scale = 5)
